package 分类.二叉树;

public class 完全二叉树的节点个数 {
    public int countNodes(TreeNode root) {
      TreeNode l=root,r=root;
      int hl=0,hr=0;
      while (l!=null){
          l=l.left;
          hl++;
      }
      while (r!=null){
          r=r.right;
          hr++;
      }
      if(hl==hr){
          return (int)Math.pow(2,hl)-1;
      }else {
          return 1+countNodes(root.left)+countNodes(root.right);
      }
    }
}
